import { expect } from 'chai';
import sinon from 'sinon';
import axios from 'axios';
import { getEndpoint } from '../../src/oss';
describe('OSS Module - getEndpoint Function', () => {
    let axiosGetStub: sinon.SinonStub;

    beforeEach(() => {
        // Mock axios.get 方法
        axiosGetStub = sinon.stub(axios, 'get');
    });

    afterEach(() => {
        // 恢复原始方法
        sinon.restore();
    });

    it('should return inputted endpoint if provided', async () => {
        const endpoint = 'custom-endpoint.aliyuncs.com';
        const result = await getEndpoint(endpoint);
        expect(result).to.equal(endpoint);
        sinon.assert.notCalled(axiosGetStub); // 确保未调用 axios.get
    });

    it('should return internal endpoint if accessible', async () => {
        const internalEndpoint = 'oss-cn-beijing-internal.aliyuncs.com';
        process.env['FLOW_OSS_ENDPOINT_INTERNAL'] = internalEndpoint;

        // Mock axios.get 成功响应
        axiosGetStub.resolves({ status: 200 });

        const result = await getEndpoint('');
        expect(result).to.equal(internalEndpoint);
        sinon.assert.calledOnceWithExactly(axiosGetStub, `https://${internalEndpoint}`, { timeout: 1000 });
    });

    it('should fallback to internet endpoint if internal fails with non-403 error', async () => {
        const internalEndpoint = 'oss-cn-beijing-internal.aliyuncs.com';
        const internetEndpoint = 'oss-cn-beijing.aliyuncs.com';
        process.env['FLOW_OSS_ENDPOINT_INTERNAL'] = internalEndpoint;
        process.env['FLOW_OSS_ENDPOINT_INTERNET'] = internetEndpoint;

        // Mock axios.get 抛出非 403 错误
        axiosGetStub.rejects({ response: { status: 500 } });

        const result = await getEndpoint('');
        expect(result).to.equal(internetEndpoint);
        sinon.assert.calledOnceWithExactly(axiosGetStub, `https://${internalEndpoint}`, { timeout: 1000 });
    });

    it('should use internal endpoint if error status is 403', async () => {
        const internalEndpoint = 'oss-cn-beijing-internal.aliyuncs.com';
        process.env['FLOW_OSS_ENDPOINT_INTERNAL'] = internalEndpoint;

        // Mock axios.get 抛出 403 错误
        const axiosError = {
            status: 403,
            isAxiosError: true,
            toJSON: () => ({}),
        };

        axiosGetStub.rejects(axiosError);
        const result = await getEndpoint('');
        expect(result).to.equal(internalEndpoint);
        sinon.assert.calledOnceWithExactly(axiosGetStub, `https://${internalEndpoint}`, { timeout: 1000 });
    });

    it('should use accelerate endpoint if useForeignCluster is true and internal fails', async () => {
        const internalEndpoint = 'oss-cn-beijing-internal.aliyuncs.com';
        process.env['FLOW_OSS_ENDPOINT_INTERNAL'] = internalEndpoint;
        process.env['useForeignCluster'] = 'true';

        // Mock axios.get 抛出错误
        axiosGetStub.rejects({ response: { status: 500 } });

        const result = await getEndpoint('');
        expect(result).to.equal('oss-accelerate.aliyuncs.com');
        sinon.assert.calledOnceWithExactly(axiosGetStub, `https://${internalEndpoint}`, { timeout: 1000 });
    });
});
